package com.yqg.education.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.controller.dto.FileDTO;
import com.yqg.education.entity.Course;
import com.yqg.education.entity.TeachPlan;
import com.yqg.education.mapper.CourseMapper;
import com.yqg.education.mapper.TeachPlanMapper;
import com.yqg.education.service.ICourseService;
import com.yqg.education.service.ITeachPlanService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CourseController 前端控制器
 *
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    private ICourseService courseService;

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private TeachPlanMapper teachPlanMapper;

    @Resource
    private ITeachPlanService teachPlanService;
    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    public Result save(@RequestBody Course course) {
        course.setCreateTime(LocalDateTime.now());
        courseService.saveOrUpdate(course);
        LambdaQueryWrapper<TeachPlan> teachPlanQueryWrapper = new LambdaQueryWrapper<>();
        teachPlanQueryWrapper.eq(TeachPlan::getCourseId, course.getId());
        teachPlanMapper.delete(teachPlanQueryWrapper);
        if (course.getTeachPlans() != null && course.getTeachPlans().size() > 0) {
            List<FileDTO> fileDTOS = course.getTeachPlans();
            ArrayList<TeachPlan> teachPlans = new ArrayList<>();
            for (FileDTO fileDTO:fileDTOS) {
                TeachPlan teachPlan = new TeachPlan();
                BeanUtil.copyProperties(fileDTO, teachPlan, true);
                teachPlan.setCourseId(course.getId());
                teachPlan.setIsDelete(0);
                teachPlan.setCreateTime(LocalDateTime.now());
                teachPlans.add(teachPlan);
            }
            teachPlanService.saveBatch(teachPlans);
        }
        return Result.success();
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Course course = courseService.getById(id);
        course.setIsDelete(1);
        return Result.success(courseService.saveOrUpdate(course));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<Course> list = courseService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(courseService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name,
                           @RequestParam(required = false) Integer typeId) {
        pageNum = pageNum * pageSize - pageSize;
        List<Course> courseList = courseMapper.findPage(pageNum, pageSize, name, typeId);
        Long count = courseMapper.findCount(name, typeId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", courseList);
        map.put("total", count);
        return Result.success(map);
    }

    @GetMapping("/findPageByClassId")
    public Result findPageByClassId(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name,
                           @RequestParam(required = false) Integer typeId,
                           @RequestParam Integer classId) {
        pageNum = pageNum * pageSize - pageSize;
        List<Course> courseList = courseMapper.findPageByClassId(pageNum, pageSize, name, typeId, classId);
        Long count = courseMapper.findCountByClassId(name, typeId, classId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", courseList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 学员端查询课程分页查询课程视频数据
     */
    @GetMapping("stuPage")
    public Result findStuPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(required = false) String name,
                              @RequestParam(required = false) Integer typeId,
                              @RequestParam Integer userId){

        return Result.success(courseService.findStuPage(pageNum,pageSize,name,typeId,userId));
    }
    /**
     * 后端首页信息查询
     */
    @GetMapping("/findIndexInfo")
    public Result findIndexInfo(){

        Map<String,Object> indexInfo = courseService.findIndexInfo();
        return Result.success(indexInfo);
    }

    @GetMapping("/getCourseByClassId")
    public Result getCourseByClassId(Long classId) {
        return Result.success(courseMapper.getCourseByClassId(classId));
    }


    /**
     * 根据班级号查询未绑定的所以课程
     */
    @GetMapping("/getAllUnboundCourseByClass")
    public Result getAllUnboundCourseByClass(@RequestParam(required = false) Long classId) {
        List<Course> list = courseMapper.getAllUnboundCourseByClass(classId);
        return Result.success(list);
    }
}

